package com.yyx.app1.CommonControls.MyViewPager;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import org.androidannotations.annotations.EView;

/**
 * Created by yuexing on 2015/10/23.
 */
@EView
public class MyViewPager extends ViewPager {
    private int oldPosition;
    /** 进度条宽度 **/
    private Integer imageViewW = 0;
    private int viewPagerW;
    private LinearLayout linearLayout;

    public MyViewPager(Context context) {
        super(context);
    }

    public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void init(int viewPagerChildAmount,int viewPagerWidth,LinearLayout linearLayout, final BackButton[] selectButtons,
                     final int begincolor, final int aftercolor, final int[] selectImages,final int[] unSelectImages) throws Exception
    {
        Exception e;
        this.viewPagerW = viewPagerWidth;
        this.imageViewW = viewPagerWidth/viewPagerChildAmount;
        this.linearLayout = linearLayout;
        if (selectButtons.length==viewPagerChildAmount)
        {
            this.addOnPageChangeListener(new OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    tabMove(position,positionOffsetPixels);
                }

                @Override
                public void onPageSelected(int position) {
                    if (selectButtons[position] == null)
                    {
                        Log.e("异常1510231549","空指针");
                        return;
                    }
                    selectButtons[position].getText().setTextColor(aftercolor);
                    selectButtons[position].getImage().setImageResource(selectImages[position]);
                    selectButtons[oldPosition].getText().setTextColor(begincolor);
                    selectButtons[oldPosition].getImage().setImageResource(unSelectImages[oldPosition]);
                    oldPosition=position;
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
            for (int i = 0 ; i <selectButtons.length ; ++i)
            {
                selectButtons[i].setTag(i);
                selectButtons[i].setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MyViewPager.this.setCurrentItem((int)v.getTag());
                    }
                });
            }
        }
        else
        {
            e = new Exception("viewPagerChildAmount数量不正确");
            throw e;
        }
    }
    /**
     * 进度条移动 :核心的移动算法
     *
     * @param position
     *            当前页位置
     * @param positionOffsetPixels
     *            移动像素值
     */
    private void tabMove(int position, int positionOffsetPixels) {
        try
        {
            int width = (int) ( (imageViewW * position) + (((double) positionOffsetPixels / viewPagerW) * imageViewW));
            linearLayout.setPadding(width,0,0,0); /** 进度条偏移量 **/
        }
        catch (Exception e)
        {
            Log.e("异常1510231821",e.toString());
        }
    }
}
